package org.example.linkedlist;

public class demo3 {
    static class ListNode {
     int val;
     ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }
    public ListNode middleNode(ListNode head) {
        // 一个一次走一步,一个一次走两步,即可实现.
        if(head==null || head.next==null){
            return head;
        }
        ListNode head1=head;
        ListNode head2=head;
        while(head2!=null&&head2.next!=null){
            head1=head1.next;
            head2=head2.next.next;
        }
        return head1;
    }
}
